package com.iking.housekeeping.controller.elderly;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.iking.housekeeping.entity.dept.SysDept;
import com.iking.housekeeping.service.dept.DeptService;
import com.iking.housekeeping.service.user.LoginService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.iking.housekeeping.common.CodeEnum;
import com.iking.housekeeping.common.PageResult;
import com.iking.housekeeping.common.ReturnCode;
import com.iking.housekeeping.entity.elderly.Elderly;
import com.iking.housekeeping.entity.elderlyrelation.ElderlyRelation;
import com.iking.housekeeping.httpbean.elderlyrelation.HttpElderlyRelation;
import com.iking.housekeeping.httpbean.elderlyreq.ElderlySelectReq;
import com.iking.housekeeping.httpbean.elderlyreq.HttpElderlyReq;
import com.iking.housekeeping.httpbean.elderlyres.ElderlyRes;
import com.iking.housekeeping.httpbean.elderlyres.HttpElderlyRes;
import com.iking.housekeeping.httpbean.list.Gender;
import com.iking.housekeeping.httpbean.servicestaff.reqbean.HttpGiveElderly;
import com.iking.housekeeping.service.elderly.ElderlyService;
import com.iking.housekeeping.service.elderlyrelation.ElderlyRelationService;
import com.iking.housekeeping.util.DateUtil;
import com.iking.housekeeping.util.UUIDUtil;
import com.iking.housekeeping.util.token.WebTokenUtil;

/**
 * 老人
 * 
 * @author: gf
 * @date: 2018年4月18日 上午11:54:44
 */
@RestController
@RequestMapping("/enderly")
public class ElderlyControler {

	/**
	 * 老人管理
	 */
	@Autowired
	private ElderlyService elderlyService;

	/**
	 * 家属管理
	 */
	@Autowired
	private ElderlyRelationService elderlyRelationService;

	@Autowired
	private DeptService deptService;
	@Autowired
	private LoginService loginService;

	/**
	 * 添加老人信息
	 * 
	 * @param httpElderlyReq
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/insert")
	public ReturnCode insert(@RequestBody HttpElderlyReq httpElderlyReq, BindingResult bindingResult) {
		// 验证参数
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			return new ReturnCode(CodeEnum.FAIL_EARROR_PARM, msg, null);
		}
		// 验证姓名是否带空格
		if (httpElderlyReq.getfName().contains(" ")) {
			return new ReturnCode(CodeEnum.FAIL, "姓名不能包含空格！", null);
		}
		// 验证电话是否带空格
		if (httpElderlyReq.getfPhone().contains(" ")) {
			return new ReturnCode(CodeEnum.FAIL, "电话不能包含空格！", null);
		}
		// 验证身份证是否带空格
		if (httpElderlyReq.getfCardId().contains(" ")) {
			return new ReturnCode(CodeEnum.FAIL, "身份证不能包含空格！", null);
		}
		// 验证身份证是否已存在
//		if (elderlyService.selectByCardId(httpElderlyReq.getfCardId()) != null) {
//			return new ReturnCode(CodeEnum.FAIL, "身份证已存在！", null);
//		}
		// 验证电话是否重复
//		if (elderlyService.selectByPhone(httpElderlyReq.getfPhone()) != null) {
//			return new ReturnCode(CodeEnum.FAIL, "电话重复！", null);
//		}
		// 接受参数值
		Elderly elderly = new Elderly(httpElderlyReq);
		// 老人添加
		elderlyService.insert(elderly);

		List<HttpElderlyRelation> list = httpElderlyReq.getList();
		// 添加家属
		for (HttpElderlyRelation httpElderlyRelation : list) {
			ElderlyRelation elderlyRelation = new ElderlyRelation();
			elderlyRelation.setfId(UUIDUtil.getUUID());
			elderlyRelation.setfIsdelete(false);
			elderlyRelation.setfName(httpElderlyRelation.getfName());
			// 设置家属中的老人
			elderlyRelation.setfElderly(elderly.getfId());
			elderlyRelation.setfGender(httpElderlyRelation.getfGender());
			elderlyRelation.setfPhone(httpElderlyRelation.getfPhone());
			elderlyRelation.setfRelation(httpElderlyRelation.getfName());
			elderlyRelation.setfAddress(httpElderlyRelation.getfAddress());
			elderlyRelationService.insert(elderlyRelation);
		}
		return new ReturnCode(CodeEnum.SUCCESS, elderly);
	}

	/**
	 * 批量删除老人
	 * 
	 * @param list
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/deleteBatch")
	public ReturnCode deleteBatch(@RequestBody List<String> list) {
		String fState = "795b6e37d8fa45e489e8e8709865a131";
		// 查询没有正在服务的老人信息
		List<String> serviceStaffListNo = elderlyService.selectElderlyBySelectList(list, fState);
		if (serviceStaffListNo.size() > 0) {
			String msg = StringUtils.join(serviceStaffListNo.toArray(), ",") + "有正在进行的服务，不可删除！";
			return new ReturnCode(CodeEnum.FAIL, msg, null);
		}
		int result = elderlyService.deleteBatch(list);
		if (result == 0) {
			return new ReturnCode(CodeEnum.FAIL, "删除失败", null);
		}
		elderlyService.deleteBatch(list);
		return new ReturnCode(CodeEnum.SUCCESS);
	}

	/**
	 * 更新老人信息
	 * 
	 * @param httpElderlyReq
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/update")
	public ReturnCode update(@RequestBody HttpElderlyReq httpElderlyReq, BindingResult bindingResult) {
		// 验证参数
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			return new ReturnCode(CodeEnum.FAIL, msg);
		}
		// 验证姓名是否带空格
		if (httpElderlyReq.getfName().contains(" ")) {
			return new ReturnCode(CodeEnum.FAIL, "姓名不能包含空格！", null);
		}
		// 验证电话是否带空格
		if (httpElderlyReq.getfPhone().contains(" ")) {
			return new ReturnCode(CodeEnum.FAIL, "电话不能包含空格！", null);
		}
		// 验证身份证是否带空格
		if (httpElderlyReq.getfCardId().contains(" ")) {
			return new ReturnCode(CodeEnum.FAIL, "身份证不能包含空格！", null);
		}

		// 查询对象
		Elderly elderlyInfo = elderlyService.selectByParmaryKey(httpElderlyReq.getfId());
		// 验证身份证是否已存在
//		if (elderlyInfo != null) {
//			if (!elderlyInfo.getfCardId().equals(httpElderlyReq.getfCardId())) {
//				if (httpElderlyReq != null) {
//					return new ReturnCode(CodeEnum.FAIL, "身份证已存在！", null);
//				}
//			}
//		}
		// 通过老人的id查询家属(通过页面传来的老人id，删除，在接受页面的老人信息)
		List<ElderlyRelation> list = elderlyRelationService.selectByEldId(httpElderlyReq.getfId());
		if (list != null && list.size() > 0) {
			// 删除家属
			elderlyRelationService.deleteByElderly(elderlyInfo.getfId());
		}

		// 接受参数
		Elderly elderly = new Elderly(httpElderlyReq);
		// 老人添加
		elderlyService.update(elderly);

		List<HttpElderlyRelation> list2 = httpElderlyReq.getList();
		// 添加家属
		for (HttpElderlyRelation httpElderlyRelation : list2) {
			ElderlyRelation elderlyRelation = new ElderlyRelation();
			elderlyRelation.setfId(UUIDUtil.getUUID());
			elderlyRelation.setfIsdelete(false);
			elderlyRelation.setfName(httpElderlyRelation.getfName());
			// 设置家属中的老人
			elderlyRelation.setfElderly(elderly.getfId());
			elderlyRelation.setfGender(httpElderlyRelation.getfGender());
			if(httpElderlyRelation.getfGender() == null) {
				elderlyRelation.setfGender("");
			}
			elderlyRelation.setfPhone(httpElderlyRelation.getfPhone());
			elderlyRelation.setfRelation(httpElderlyRelation.getfName());
			elderlyRelation.setfAddress(httpElderlyRelation.getfAddress());
			elderlyRelationService.insert(elderlyRelation);
		}
		return new ReturnCode(CodeEnum.SUCCESS);
	}

	/**
	 * 分页查询老人(响应)
	 * 
	 * @param elderlyPage
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/selectAll")
	public ReturnCode selectAll(@RequestBody ElderlySelectReq elderlyPage, HttpServletRequest request) {
		// 获取当前登录人id
		String userId = WebTokenUtil.getUserIdByRequest(request);
		List<SysDept> menuList = deptService.selectAdminById(userId);
		int result=loginService.select(elderlyPage.getfStreetCommunity(),menuList);
		if(result==0){
			return new ReturnCode(CodeEnum.FAIL,"您没有该区域查看权限！", null);
		}else if(result==1){
				elderlyPage.setfStreetCommunity(menuList.get(0).getfId());
		}else if(result==2){
			elderlyPage.setfStreetCommunity(menuList.get(0).getfId());
		}else if(result==3) {
			elderlyPage.setfStreetCommunity(menuList.get(0).getfId());
		}
		// 执行方法
		Page<HttpElderlyRes> page = elderlyService.selectAll(elderlyPage);
		return new ReturnCode(CodeEnum.SUCCESS, new PageResult<HttpElderlyRes>(page));
	}

	/**
	 * 性别列表
	 * 
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/selectGender")
	public ReturnCode selectGender() {
		List<Gender> selectGender = elderlyService.selectGender();
		return new ReturnCode(CodeEnum.SUCCESS, selectGender);
	}

	/**
	 * 
	 * @Description:根据服人员id查询老人信息
	 * @author:CGZ
	 * @return
	 */
	@GetMapping("/information")
	public ReturnCode information(HttpServletRequest request) {
		String userId = WebTokenUtil.getUserIdByRequest(request);
		List<Elderly> result = elderlyService.getInformation(userId);
		if (result == null || result.size() == 0) {
			return new ReturnCode(CodeEnum.SUCCESS, null);
		}
		return new ReturnCode(CodeEnum.SUCCESS, result);
	}

	/**
	 * 老人详情
	 * 
	 * @param fId
	 * @return ReturnCode
	 * @author gf
	 */
	@GetMapping("/selectElderlyDetails/{fId}")
	public ReturnCode selectElderlyDetails(@PathVariable("fId") String fId) {
		// 详情对象
		HttpElderlyReq selectElderlyDetails = elderlyService.selectByEldId(fId);
		return new ReturnCode(CodeEnum.SUCCESS, "老人详情查询", selectElderlyDetails);
	}

	/**
	 * 
	 * @Description:查询当前预约老人
	 * @author:CGZ
	 * @param request
	 * @return
	 */
	@GetMapping("/selectAllElderly")
	public ReturnCode selectElderlyDetails(HttpServletRequest request) {
		List<ElderlyRes> selectElderlyDetails = elderlyService.selectAllElderly(request);
		return new ReturnCode(CodeEnum.SUCCESS, selectElderlyDetails);
	}

	/**
	 * 展示分配人员页面
	 * 
	 * @param httpGiveElderly
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/giveServiceStaff")
	public ReturnCode giveServiceStaff(@RequestBody HttpGiveElderly httpGiveElderly) {
		// 查询没有正在服务的老人信息
		List<String> fId=httpGiveElderly.getfElderlyId();
	    List<String>  serviceStaffListNo = elderlyService.selectElderlyState(fId);
	if(serviceStaffListNo.size() < 1){
			int result = elderlyService.giveServiceStaff(httpGiveElderly);
			return new ReturnCode(CodeEnum.SUCCESS, "成功分配给" + result + "老人", null);
	}
		return new ReturnCode(CodeEnum.FAIL, "该老人已被预约，请结束或取消当前预约！", null);
	}
	/**
	 * 查询老人在某时间段的服务信息
	 * @param httpElderlyRes
	 * @return
	 */
	@PostMapping("/selectInfoByTime")
	public ReturnCode  selectInfoByTime( @RequestBody HttpElderlyRes httpElderlyRes){
		if(httpElderlyRes.getfBeginnTime() != null) {
			httpElderlyRes.setfBeginnTime(DateUtil.addStartTime(httpElderlyRes.getfBeginnTime()));
		}
		if(httpElderlyRes.getfEndTime() != null) {
			httpElderlyRes.setfEndTime(DateUtil.getDayAfter(DateUtil.addStartTime(httpElderlyRes.getfEndTime())));
		}
		List<HttpElderlyRes> list=elderlyService.selectInfoByTime(httpElderlyRes);
		return new ReturnCode(CodeEnum.SUCCESS, list);
	}
}
